package com.hm.cms.websit;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hm.cms.service.ArticleService;
import com.hm.cms.service.TermsService;
import com.hm.cms.util.WebSitUtil;
import com.hm.cms.vo.HcmsArticleVo;
import com.hm.cms.vo.HcmsTermsVo;
import com.hm.common.util.CommonUtil;
import com.hm.common.util.StringUtil;
import com.hm.common.util.WebUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2017年11月19日 上午2:17:47
 * @version 1.0
 * @describe
 */
@Slf4j
@Component
public class JdonHandler {

	private static int timeout = 3000000;
	
	@Autowired
	private TermsService termsService;

	@Autowired
	private ArticleService articleService;

	public void handler(String operationUserId) {
		AtomicLong index = new AtomicLong(articleService.getArtilcleMaxOrder() + 1);
		for (int i = 0; i <= 12; i++) {
			loadTags(i, operationUserId, index,"5a19124e46c46ba8b7d06bdc");
			WebSitUtil.wating3600();
		}
	}

	private void loadArticle(String title, String uri, AtomicLong index, String operationUserId, String tagId) {
		try {
			System.out.println("文章"+uri);
			if (articleService.checkArticleSourceUri(uri)) {
				return;
			}

			Document document = Jsoup.connect(uri).ignoreContentType(true)
					.userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)").get();

			String description = document.select("meta[name=description]").get(0).attr("content");
			String keyStr = document.select("meta[name=keywords]").get(0).attr("content");
			List<String> termIds = new ArrayList<String>();
			String[] keys = keyStr.split(",");
			for (String key : keys) {
				if (CommonUtil.isEmpty(key)) {
					continue;
				}
				HcmsTermsVo hcmsTermsVo = termsService.getHcmsTermsByTermName(key);
				if (CommonUtil.isNotEmpty(hcmsTermsVo)) {
					termIds.add(hcmsTermsVo.getId());
					continue;
				}

				HcmsTermsVo termsVo = new HcmsTermsVo();
				termsVo.setTermName(key);
				termsVo.setTermCode(key);
				termsVo.setTermOrder(-1);
				termIds.add(termsService.addTerms(termsVo, operationUserId));
			}

			String content = document.getElementsByClass("tpc_content").get(0).html();
			if (StringUtil.isBlankOrNull(description)) {
				description = StringUtil.dislodgeAllBlank(WebUtil.clearHtmlTags(content)).substring(0, 150);
			}

			HcmsArticleVo articleVo = new HcmsArticleVo();
			{
				articleVo.setTermIds(termIds);
				articleVo.setTermTayIds(Arrays.asList(tagId));
				articleVo.setArticleContent(content);
				articleVo.setAuthorName("网络采集");
				articleVo.setReleaseTime(System.currentTimeMillis());
				articleVo.setTitle(title);
				articleVo.setKeywords(StringUtil.isBlankOrNull(keyStr) ? title : keyStr);
				articleVo.setSourceName("jdon.com");
				articleVo.setSourceUri(uri);
				articleVo.setArticleOrder((int) index.incrementAndGet());
				articleVo.setArticleExcerpt(description);
			}
			articleService.addArticle(articleVo, operationUserId);
			System.out.println("文章"+content);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception {

		String uri = "http://www.jdon.com/49081";
		Document document = Jsoup.connect(uri).data("query", "Java").userAgent("Mozilla").cookie("auth", "token").timeout(timeout).get();

		System.out.println(document.getElementsByClass("tpc_content").get(0));
	}

	private void loadMainDirectory(String uri, String operationUserId, AtomicLong index, String tagId) {
		try {
			String host = "http://www.jdon.com";

			// String uri = "http://www.jdon.com/tags/272";
			System.out.println("主目录："+uri);
			for (int i = 0; i < 24; i++) {
				Document document = Jsoup.connect(uri + "/" + i).ignoreContentType(true)
						.userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)").get();

				Elements elements = document.getElementsByClass("post-headline");
				for (Element el : elements) {
					String title = el.getElementsByTag("h3").html();
					String path = host + el.getElementsByTag("a").get(0).attr("href");
					loadArticle(title, path, index, operationUserId, tagId);
					WebSitUtil.wating3();
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}

	private void loadTags(int page, String operationUserId, AtomicLong index, String tagId) {
		try {
			String host = "http://www.jdon.com";

			// 50
			String uri = "http://www.jdon.com/tags/p/" + (page * 50);
			Document document = Jsoup.connect(uri).ignoreContentType(true)
					.userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)").get();

			Elements elements = document.getElementsByClass("post-tag");
			for (Element el : elements) {
				loadMainDirectory(host + el.attr("href"), operationUserId, index, tagId);
				WebSitUtil.wating3();
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}
}
